
package com.x52im.mall.logic.shop;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.eva.framework.dto.DataFromServer;
import com.x52im.mall.IntentFactory;
import com.x52im.mall.ObjectFactory;
import com.x52im.mall.shop.dto.SOConsigneeInfo;
import com.x52im.rbchat.R;
import com.x52im.mall.MallGlobal;
import com.x52im.mall.Utility;
import com.eva.android.widget.DataLoadableActivity;
import com.eva.android.widget.DataRender;
import com.eva.android.widget.RenderedSpinnerAdapter;
import com.eva.android.widget.WidgetUtils;

/**
 * 
 * @author Jason
 * 
 */
public class OrderConfirmShippingAddressActivity extends DataLoadableActivity
{
	/** 从Intent传过来的用于初始化的数据 */
	private SOConsigneeInfo consigneeInfoForInit = null;
	
	private EditText sunameTextView;
	private EditText nameTextView;
	private Spinner countrySpinner;
	private EditText cityTextView;
	private EditText addressTextView;
	private EditText codeTextView;
	private EditText mailTextView;
	private EditText phoneTextView;
	private EditText appenderTextView;
	private Button sureBtn;
	
	private SOConsigneeInfo ci = null;

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void initDataFromIntent()
	{
		//解析从intent中传过来的数据
		consigneeInfoForInit = IntentFactory.parseOrderConfirmShippingAddressActivityIntent(getIntent());
	}

	@Override
	protected void initViews(Bundle savedInstanceState)
	{
		customeTitleBarResId = R.id.common_mall_shop_layout_shipping_address_titleBar;
		// 首先设置contentview
		setContentView(R.layout.common_mall_shop_layout_shipping_address);

		setTitle(R.string.common_mall_consignee_info_title);
		
		sunameTextView = (EditText) this.findViewById(R.id.common_mall_shop_layout_shipping_address_suname);
		nameTextView = (EditText) this.findViewById(R.id.common_mall_shop_layout_shipping_address_name);
		cityTextView = (EditText) this.findViewById(R.id.common_mall_shop_layout_shipping_address_city);
		addressTextView = (EditText) this.findViewById(R.id.common_mall_shop_layout_shipping_address_address);
		codeTextView = (EditText) this.findViewById(R.id.common_mall_shop_layout_shipping_address_code);
		mailTextView = (EditText) this.findViewById(R.id.common_mall_shop_layout_shipping_address_email);
		phoneTextView = (EditText) this.findViewById(R.id.common_mall_shop_layout_shipping_address_phone);
		appenderTextView = (EditText) this.findViewById(R.id.common_mall_shop_layout_shipping_address_appender);

		countrySpinner = ObjectFactory.createCountrySpinner(this, R.id.common_mall_shop_layout_shipping_address_country);

		sureBtn = (Button) this.findViewById(R.id.common_mall_shop_layout_shipping_address_sureBtn);
		
		mailTextView.setText(MallGlobal.getMallProviderInstance(this).getRobotimeMall().getLoginedUserMail());
		//
//		initForModify();
		// 异步加载render：Android自2.3及以后版后，要求网络交互必需要放在异步线程里处理！
		new WidgetUtils.RenderedSpinnerCreatorAsync(this, countrySpinner){
			@Override
			protected DataRender fetchDataRender()
			{
				return ObjectFactory.createCountryRenderer(OrderConfirmShippingAddressActivity.this);
			}
			
			protected void onPostExecute(DataRender dr)
			{
				super.onPostExecute(dr);
				initForModify();
			}
		}.execute();
	}

	@Override
	protected void initListeners()
	{
		sureBtn.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				if (chkViewTextValidate())
				{
					Intent intent = new Intent();
					intent.putExtra(OrderComfirmActivity.INTENT_KEY_EXTRA_CONSIGNEE$INFO$DTO, getConsigneeInfo());
					setResult(Activity.RESULT_OK, intent);
					finish();
				}
			}
		});
	}
	
	private void initForModify()
	{
		if(consigneeInfoForInit != null)
		{
			sunameTextView.setText(consigneeInfoForInit.getConsignee_surname());
			nameTextView.setText(consigneeInfoForInit.getConsignee_name());
			((RenderedSpinnerAdapter)countrySpinner.getAdapter()).setSelectedItem(
								consigneeInfoForInit.getCountry_of_consignee());
			cityTextView.setText(consigneeInfoForInit.getCity_of_consignee());
			addressTextView.setText(consigneeInfoForInit.getAddr_of_consignee());
			mailTextView.setText(consigneeInfoForInit.getMail_of_consignee());
			codeTextView.setText(consigneeInfoForInit.getCode_of_consignee());
			phoneTextView.setText(consigneeInfoForInit.getPhone_of_consignee());
			appenderTextView.setText(consigneeInfoForInit.getAppender());
		}
	}

	private SOConsigneeInfo getConsigneeInfo()
	{
		ci = new SOConsigneeInfo();
		ci.setConsignee_surname(sunameTextView.getText().toString());
		ci.setConsignee_name(nameTextView.getText().toString());
		ci.setCountry_of_consignee(((RenderedSpinnerAdapter) countrySpinner.getAdapter()).getSelectedKey().toString());
		ci.setCity_of_consignee(cityTextView.getText().toString());
		ci.setAddr_of_consignee(addressTextView.getText().toString());
		ci.setMail_of_consignee(mailTextView.getText().toString());
		ci.setCode_of_consignee(codeTextView.getText().toString());
		ci.setPhone_of_consignee(phoneTextView.getText().toString());
		ci.setAppender(appenderTextView.getText().toString());
		return ci;
	}

	private boolean ViewTextIsNull(EditText view)
	{
		if (view.getText() != null && !"".equals(view.getText().toString().trim()))
			return false;
		return true;
	}

	private boolean chkViewTextValidate()
	{
		if (ViewTextIsNull(sunameTextView))
		{
			Toast.makeText(OrderConfirmShippingAddressActivity.this, R.string.common_mall_consignee_info_address_suname_validate, Toast.LENGTH_LONG).show();
			return false;
		}
		else if (ViewTextIsNull(nameTextView))
		{
			Toast.makeText(OrderConfirmShippingAddressActivity.this, R.string.common_mall_consignee_info_address_name_validate, Toast.LENGTH_LONG).show();
			return false;
		}
		else if (ViewTextIsNull(cityTextView))
		{
			Toast.makeText(OrderConfirmShippingAddressActivity.this, R.string.common_mall_consignee_info_address_city_validate, Toast.LENGTH_LONG).show();
			return false;
		}
		else if (ViewTextIsNull(addressTextView))
		{
			Toast.makeText(OrderConfirmShippingAddressActivity.this, R.string.common_mall_consignee_info_address_addr_validate, Toast.LENGTH_LONG).show();
			return false;
		}
		else if (ViewTextIsNull(codeTextView))
		{
			Toast.makeText(OrderConfirmShippingAddressActivity.this, R.string.common_mall_consignee_info_address_code_validate, Toast.LENGTH_LONG).show();
			return false;
		}
		else if (ViewTextIsNull(mailTextView))
		{
			Toast.makeText(OrderConfirmShippingAddressActivity.this, R.string.common_mall_consignee_info_address_mail_validate_isnull, Toast.LENGTH_LONG).show();
			return false;
		}
		else if (ViewTextIsNull(phoneTextView))
		{
			Toast.makeText(OrderConfirmShippingAddressActivity.this, R.string.common_mall_consignee_info_address_phone_validate, Toast.LENGTH_LONG).show();
			return false;
		}
		else if (!Utility.isEmail(mailTextView.getText().toString()))
		{
			Toast.makeText(OrderConfirmShippingAddressActivity.this, R.string.common_mall_consignee_info_address_mail_validate_isok, Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}

	/* (non-Javadoc)
	 * @see com.eva.android.widget.DataLoadableActivity#queryData(java.lang.String[])
	 */
	@Override
	protected DataFromServer queryData(String... arg0)
	{
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.eva.android.widget.DataLoadableActivity#refreshToView(java.lang.Object)
	 */
	@Override
	protected void refreshToView(Object arg0)
	{
		// TODO Auto-generated method stub
	}

}
